﻿param(
	[string]$installationUrl = $(throw "Please specify the url of your SharePoint Mysite Host Site Collection")
)
"Uninstall Invoked..."

### DEFINE VARIABLES FOR SCRIPT ##
$SiteUrl = $installationUrl
#$SiteUrl = "http://st-egov-dev01:2551/my"
$SolutionName = “eGov.SharePoint.MyPeeps.wsp“
$MySiteFeatureID = "cd8bf2b6-5a4c-4ff8-bb87-f938271a52ed"
$MySiteHostFeatureID = "5e311e70-08dd-43d5-9f12-fcc9b2f7f0cd"
$StaplerFeatureID = "d4e27b31-d3ab-41af-9496-1842f571d550"

## END VARIABLE DEFINITION ##

	##### BEGIN UNINSTALL SCRIPT #####


Try
{
	# Ensure Microsoft.SharePoint.PowerShell is loaded
	$snapin = Get-PSSnapin | Where-Object {$_.Name -eq ‘Microsoft.SharePoint.Powershell’}

	if ($snapin -eq $null)
	{
		“***Loading SharePoint Powershell”
		Add-PSSnapin “Microsoft.SharePoint.Powershell”
	}

	$site = Get-SPSite $SiteUrl
	$MySiteWebApplication = $Site.WebApplication
	$SiteCollection = $MySiteWebApplication.Sites

	# Check if Solution Exists
	$isSolutionInStore = $null	
    $WSP = Get-SPSolution | Where {($SolutionName -eq $_.Name)}  
    if($WSP -eq $null) 
		{ $isSolutionInStore = 0 }  
    else 
		{ $isSolutionInStore = 1 } 


	if ($isSolutionInStore -eq 1) 
	{
		##### De-activating Features  ######

	
		$RootWeb = $site.OpenWeb("")

		"*** De-Activating eGov MyPeeps MySite 'Host' Customization"
		if($RootWeb.WebTemplate-eq "SPSMSITEHOST")
		{
			#De-Activate MySite Host Feature
			$WebUrl = $RootWeb.Url
			Try
			{
				$Feature  = Get-SPFeature -Identity $MySiteHostFeatureID -site $site
			}
			Catch
			{
				$Feature = $null
			}

			if($Feature -ne $null)
			{
				"   * De-Activating eGov MyPeeps MySite 'Host' Customization Feature on $SiteUrl "
				Disable-SPFeature -url $SiteUrl -Identity $MySiteHostFeatureID -Force -confirm:$false
			}else
			{
				"   * MySite 'Host' Fetaure not activated on this site: $WebUrl"
			}
		}

		"*** De-Activating eGov MyPeeps MySite Customization on existing user mysites"
		for ($i=0; $i -lt $SiteCollection.Count; $i++ )
		{
			$RootWeb = $SiteCollection[$i].OpenWeb("")
			$WebUrl = $RootWeb.Url
			if($RootWeb.WebTemplate -eq "SPSPERS")
			{
				#TODO Activate MySite Feature
				Try
				{
					$Feature  = Get-SPFeature -Identity $MySiteFeatureID -site $SiteCollection[$i]
				}
				Catch
				{
					$Feature = $null
				}
				if($Feature -ne $null)
				{
					"   * De-Activating eGov-MyPeeps MySite Customization Feature on $RootWeb"
					Disable-SPFeature -url $RootWeb.Url -Identity $MySiteFeatureID -force -confirm:$false
				}
				else
				{
					"   * MySite Fetaure not activated on site: $WebUrl " 
				}
			}

			$RootWeb.Dispose()
		}


		##### Uninstalling existing Solution Package ######
		"*** Uninstalling existing solution package"
		Uninstall-SPSolution -Identity $SolutionName -Confirm:$false -AllWebApplications

		[xml]$val = stsadm -o displaysolution -name $SolutionName
	    while($val.Solution.SolutionDeploymentJob) 
	    {
	    	"   *Waiting for Uninstall-SPSolution job on $SolutionName"
	    	sleep 5
	    	[xml]$val = stsadm -o displaysolution -name $SolutionName
	    }
	    "*** Uninstall-SPSolution Job Compleated"

		"*** Removing existing solution package"
		Remove-SPSolution -Identity $SolutionName -Confirm:$false 

		$WSP = Get-SPSolution | Where {($SolutionName -eq $_.Name)}      
	    	if($WSP -eq $null) 
	    	{ 	
			"*** Solution Removed from the solution store successfully"
		}
		else
		{
			throw "SOLUTION DID NOT UNINSTALL CORRECTLY; Checking solution existance in the solution store after install returned 'true'"
		}


		"*** Restarting IIS"
		iisreset

		"*** Uninstall Complete"
	}
}Catch
{
	throw new-object Exception("Solution UnInstallation ERROR", $error[0].Exception)
}

	

